package com.maple.common.widget

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import com.maple.common.R
import com.maple.common.ext.screenWidth

/**
 * @author shangyf
 */
class MyDialog : Dialog {
    constructor(
        context: Context?, cancelable: Boolean,
        cancelListener: DialogInterface.OnCancelListener?
    ) : super(context!!, cancelable, cancelListener)

    override fun setTitle(title: CharSequence?) {
        super.setTitle(title)
    }

    constructor(context: Context?, theme: Int) : super(context!!, theme)
    constructor(context: Context?) : super(context!!)

    class Builder(private val context: Context) {
        private var title: String? = null
        private var message: String? = null
        private var positiveButtonText: String? = null
        private var negativeButtonText: String? = null
        private var contentView: View? = null
        private var titleVisibility = View.VISIBLE
        private var bottomViewVisibility = View.VISIBLE
        private var positiveButtonClickListener: DialogInterface.OnClickListener? = null
        private var negativeButtonClickListener: DialogInterface.OnClickListener? = null

        /**
         * Set the Dialog message from String
         *
         * @param message
         * @return
         */
        fun setMessage(message: String?): Builder {
            this.message = message
            return this
        }

        /**
         * Set the Dialog message from resource
         *
         * @param message
         * @return
         */
        fun setMessage(message: Int): Builder {
            this.message = context.getText(message) as String
            return this
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        fun setTitle(title: Int): Builder {
            this.title = context.getText(title) as String
            return this
        }

        fun setTitleVisibility(visibility: Int): Builder {
            titleVisibility = visibility
            return this
        }

        fun setBottomViewVisibility(visibility: Int): Builder {
            bottomViewVisibility = visibility
            return this
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */
        fun setTitle(title: String?): Builder {
            this.title = title
            return this
        }

        /**
         * Set a custom content view for the Dialog. If a message is set, the
         * contentView is not added to the Dialog...
         *
         * @param v
         * @return
         */
        fun setContentView(v: View?): Builder {
            contentView = v
            return this
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @param listener
         * @return
         */
        fun setPositiveButton(
            positiveButtonText: Int,
            listener: DialogInterface.OnClickListener?
        ): Builder {
            this.positiveButtonText = context
                .getText(positiveButtonText) as String
            positiveButtonClickListener = listener
            return this
        }

        /**
         * Set the positive button text and it's listener
         *
         * @param positiveButtonText
         * @param listener
         * @return
         */
        fun setPositiveButton(
            positiveButtonText: String?,
            listener: DialogInterface.OnClickListener?
        ): Builder {
            this.positiveButtonText = positiveButtonText
            positiveButtonClickListener = listener
            return this
        }

        /**
         * Set the negative button resource and it's listener
         *
         * @param negativeButtonText
         * @param listener
         * @return
         */
        fun setNegativeButton(
            negativeButtonText: Int,
            listener: DialogInterface.OnClickListener?
        ): Builder {
            this.negativeButtonText = context
                .getText(negativeButtonText) as String
            negativeButtonClickListener = listener
            return this
        }

        /**
         * Set the negative button text and it's listener
         *
         * @param negativeButtonText
         * @param listener
         * @return
         */
        fun setNegativeButton(
            negativeButtonText: String?,
            listener: DialogInterface.OnClickListener?
        ): Builder {
            this.negativeButtonText = negativeButtonText
            negativeButtonClickListener = listener
            return this
        }

        /**
         * Create the custom dialog
         */
        fun create(): MyDialog {
            val inflater: LayoutInflater = context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            // instantiate the dialog with the custom Theme
            val dialog = MyDialog(context, R.style.Theme_dialog)
            val layout: View = inflater.inflate(R.layout.dialog_commen_view, null)
            dialog.addContentView(
                layout, ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
                )
            )
            // set the dialog title
            layout.findViewById<AppCompatTextView>(R.id.title).text = title
            layout.findViewById<View>(R.id.title).visibility = titleVisibility
            layout.findViewById<View>(R.id.bottom_button_view).visibility = bottomViewVisibility
            // set the confirm button
            if (positiveButtonText != null) {
                (layout.findViewById<View>(R.id.positiveButton) as TextView).text =
                    positiveButtonText
                if (positiveButtonClickListener != null) {
                    layout.findViewById<View>(R.id.positiveButton)
                        .setOnClickListener {
                            positiveButtonClickListener?.onClick(
                                dialog,
                                DialogInterface.BUTTON_POSITIVE
                            )
                        }
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById<View>(R.id.positiveButton).visibility = View.GONE
                layout.findViewById<View>(R.id.line).visibility = View.GONE
                layout.findViewById<View>(R.id.negativeButton)
                    .setBackgroundResource(R.drawable.alertdialog_okbt_circle_bg02)
            }
            // set the cancel button
            if (negativeButtonText != null) {
                (layout.findViewById<View>(R.id.negativeButton) as TextView).text =
                    negativeButtonText
                if (negativeButtonClickListener != null) {
                    layout.findViewById<View>(R.id.negativeButton)
                        .setOnClickListener {
                            negativeButtonClickListener?.onClick(
                                dialog,
                                DialogInterface.BUTTON_NEGATIVE
                            )
                        }
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById<View>(R.id.negativeButton).visibility = View.GONE
                layout.findViewById<View>(R.id.positiveButton)
                    .setBackgroundResource(R.drawable.alertdialog_okbt_circle_bg02)
                layout.findViewById<View>(R.id.line).visibility = View.GONE
            }
            if (message != null) {
                (layout.findViewById<View>(R.id.dialog_message) as TextView).text = message
            } else if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                (layout.findViewById<View>(R.id.content) as LinearLayout)
                    .removeAllViews()
                (layout.findViewById<View>(R.id.content) as LinearLayout).addView(
                    contentView, ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                    )
                )
            }
            dialog.setContentView(layout)
            val window = dialog.window
            window!!.setGravity(Gravity.CENTER)
            val layoutParams: WindowManager.LayoutParams = window
                .attributes
            layoutParams.width = screenWidth * 5 / 6
            window.attributes = layoutParams
            return dialog
        }
    }
}
